//
//  SurnameController.m
//  projectOfTeamSample
//
//  Created by lk on 15/11/16.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "SurnameController.h"
#import "SurnameManager.h"
#import "SurnameCell.h"
#import "SurnameOfPicCell.h"
#import "SurnameOfVideoCell.h"

@interface SurnameController () <UITableViewDelegate, UITableViewDataSource>

// segmentedIndex index属性
@property (nonatomic, assign) NSUInteger segmentedIndex;

// AVPlayer属性
@property (nonatomic, strong) AVPlayer *player;

// AVPlayerLayer
@property (nonatomic, strong) AVPlayerLayer *playerLayer;

// cell属性
@property (nonatomic, strong) SurnameOfVideoCell *surnameOfVideoCell;

// cell行数的属性
@property (nonatomic, assign) NSUInteger index;

@end

@implementation SurnameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置头标题
    self.navigationItem.title = @"糗事";
    
    // 注册cell
    [self.tableView registerClass:[SurnameCell class] forCellReuseIdentifier:@"textCell"];
    [self.tableView registerClass:[SurnameOfPicCell class] forCellReuseIdentifier:@"picCell"];
    [self.tableView registerClass:[SurnameOfVideoCell class] forCellReuseIdentifier:@"videoCell"];
    
    // 设置代理
    self.tableView.delegate = self;
    
    // 设置SegmentedControl
    [self getSegmentedControl];
    
    // 给segmentedIndex初始值
    self.segmentedIndex = 0;
    
    
    
    
    
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - getSegmentedControl
- (void)getSegmentedControl
{
    // 获取设备的frame
    CGRect frame = [UIScreen mainScreen].bounds;
    
    // 初始化headerView
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, frame.size.width, 40)];
    
    // 初始化segmentControl
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:self.tableView.tableHeaderView.bounds];
    
    // 添加片段
    [segmentedControl insertSegmentWithTitle:@"精华" atIndex:0 animated:YES];
    [segmentedControl insertSegmentWithTitle:@"段子" atIndex:1 animated:YES];
    [segmentedControl insertSegmentWithTitle:@"笑图" atIndex:2 animated:YES];
    [segmentedControl insertSegmentWithTitle:@"视频" atIndex:3 animated:YES];
    
    // 设置开始默认选中的index
    // segmentedControl.selectedSegmentIndex = 0;
    
    // 给segmentControl添加点击事件
    [segmentedControl addTarget:self action:@selector(didClickAction:) forControlEvents:UIControlEventValueChanged];
    
    // 设置属性
    segmentedControl.tintColor = [UIColor grayColor];
    
    // 添加到父视图
    [self.tableView.tableHeaderView addSubview:segmentedControl];
}


#pragma mark - segmentControl的点击事件

- (void)didClickAction:(id)sender
{
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    
    // 设置self.segmentedIndex属性
    self.segmentedIndex = segmentControl.selectedSegmentIndex;
    
    switch (segmentControl.selectedSegmentIndex)
    {
        case 0:
        {
            // [self ]
        }
            break;
            
        case 1:
        {
            [self getSurnameFromeText];
        }
            
            break;
        
        case 2:
        {
            [self getSurnameFromePic];
        }
            break;
            
        case 3:
        {
            [self getSurnameFromVideo];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 调用manager的解析

// 解析文字
- (void)getSurnameFromeText
{
    // 解析
    [[SurnameManager managerDefault] getSurnameOfTextWithHandel:^{
        
        // 刷新
        [self.tableView reloadData];
        
    }];
}

// 解析图片
- (void)getSurnameFromePic
{
    
    // 解析
    [[SurnameManager managerDefault] getSurnameOfPicWithHandel:^{
       
        // 刷新
        [self.tableView reloadData];
        
    }];
}

// 解析视频
- (void)getSurnameFromVideo
{
    [[SurnameManager managerDefault] getSurnameOfVideoWithHandel:^{
        
        // 刷新
        [self.tableView reloadData];
    
    }];
}


#pragma mark - Tabel view delegate




// 点击cell的触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (self.segmentedIndex)
    {
        case 0:
            break;
            
        case 1:

            break;
            
        case 2:

            break;
            
        case 3:
        {
 
           /*
            // 设置选中cell的背景颜色
            UIView *selectedBackgroundView = [[UIView alloc] init];
            selectedBackgroundView.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = selectedBackgroundView;
            
            // 实例化model
            SurnameOfVideoInfo *surnameOfVideoInfo = [[SurnameManager managerDefault] getSurnameOfVideoInfoWithIndex:indexPath.row];
            
            // 实例化一个接单例返回回来的player的一个player
            self.player = [[AVPlayer alloc] init];
            
            // 接受单例中返回的player
            self.player = [[SurnameManager managerDefault] prepareWithIndex:indexPath.row];
            
            // 实例化AVPlayerLayer
            AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
            
            // 设置AVPlayerLayer的frame
            playerLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 20, ([UIScreen mainScreen].bounds.size.width - 20) * surnameOfVideoInfo.scale);
            
            // 将AVPlayerLayer添加到主视图
            [cell.picImage.layer addSublayer:playerLayer];
            
            // 播放视频
            [self.player play];
           */
           
            // 获取当前点击的cell
            SurnameOfVideoCell *surnameOfVideoCell = [tableView cellForRowAtIndexPath:indexPath];
            
            //  给属性赋值
            self.surnameOfVideoCell = surnameOfVideoCell;
            
            // 接受单例的值
            self.player = [[SurnameManager managerDefault] prepareWithIndex:[SurnameManager managerDefault].surnameOfVideoCount - 1 - indexPath.row];
            
            // 实例化AVPLayerLayer
            self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
            
            // 设置AVPLayerLayer的frame
            self.playerLayer.frame = surnameOfVideoCell.picImage.bounds;
            
            // 添加到父视图
            [surnameOfVideoCell.picImage.layer addSublayer:self.playerLayer];
            
            // 播放
            [self.player play];

            NSLog(@"点击cell");
           
        }
            break;
            
        default:
            break;
    }
    
    
    
}

 
 
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    switch (self.segmentedIndex)
    {
        case 0:
        {
            SurnameOfPicInfo *surnameOfPicInfo = [[SurnameManager managerDefault] getSurnameOfPicInfoWithIndex:[SurnameManager managerDefault].surnameOfPicCount - 1 - indexPath.row];
            
            CGFloat picHeight = surnameOfPicInfo.scale * ([UIScreen mainScreen].bounds.size.width - 20);
            
            return [SurnameOfPicCell CellOfHeightWithSurnameInfo:surnameOfPicInfo] + picHeight;
        }
            break;
            
        case 1:
        {
            SurnameInfo *surnameInfo = [[SurnameManager managerDefault] getSurnameInfoWithIndex:[SurnameManager managerDefault].surnameCount -1 - indexPath.row];
            
            return [SurnameCell CellOfHeightWithSurnameInfo:surnameInfo];
        }
            
            break;
            
        case 2:
        {
            
            SurnameOfPicInfo *surnameOfPicInfo = [[SurnameManager managerDefault] getSurnameOfPicInfoWithIndex:[SurnameManager managerDefault].surnameOfPicCount - 1 - indexPath.row];
            
            CGFloat picHeight = surnameOfPicInfo.scale * ([UIScreen mainScreen].bounds.size.width - 20);
            
            return [SurnameOfPicCell CellOfHeightWithSurnameInfo:surnameOfPicInfo] + picHeight;
        }
            break;
            
        case 3:
        {
            SurnameOfVideoInfo *surnameOfVideoInfo = [[SurnameManager managerDefault] getSurnameOfVideoInfoWithIndex:[SurnameManager managerDefault].surnameOfVideoCount - 1 - indexPath.row];
            
            CGFloat videoHeight = surnameOfVideoInfo.scale * ([UIScreen mainScreen].bounds.size.width - 20);
            
            return [SurnameOfVideoCell CellOfHeightWithSurnameInfo:surnameOfVideoInfo] + videoHeight;
        }
            break;
            
        default:
            break;
    }
    
    
    
    return 0;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// #warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// #warning Incomplete implementation, return the number of rows
    
    switch (self.segmentedIndex)
    {
        case 0:
            // 返回与之对应的行数
            return [SurnameManager managerDefault].surnameOfPicCount;
            break;
            
        case 1:
            // 返回与之对应的cell的行数
            return [SurnameManager managerDefault].surnameCount;
            break;
            
        case 2:
            // 返回与之对应的行数
            return [SurnameManager managerDefault].surnameOfPicCount;
            break;
            
        case 3:
            // 返回与之对应的行数
            return [SurnameManager managerDefault].surnameOfVideoCount;
            break;
            
        default:
            break;
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    // Configure the cell...
 
    switch (self.segmentedIndex)
    {
        case 0:
        {
            // 实例化cell
            SurnameOfPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"picCell" forIndexPath:indexPath];
            
            // 设置选中cell的背景颜色
            UIView *selectedBackgroundView = [[UIView alloc] init];
            selectedBackgroundView.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = selectedBackgroundView;
            
            // 获取对应的一行的模型
            SurnameOfPicInfo *surnameOfPicInfo = [[SurnameManager managerDefault] getSurnameOfPicInfoWithIndex:[SurnameManager managerDefault].surnameOfPicCount - 1 - indexPath.row];
            
            // 重写setter
            cell.surnameOfPicInfo = surnameOfPicInfo;
            
            // 返回cell
            return cell;
        }
            
            break;
            
        case 1:
        {
            // 实例化cell
            SurnameCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            
            // 设置选中cell的背景颜色
            UIView *selectedBackgroundView = [[UIView alloc] init];
            selectedBackgroundView.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = selectedBackgroundView;
            
            // 获取对应的一行的模型
            SurnameInfo *surnameInfo = [[SurnameManager managerDefault] getSurnameInfoWithIndex:[SurnameManager managerDefault].surnameCount - 1 - indexPath.row];
            
            // 调用重写setter方法
            cell.surnameInfo = surnameInfo;
            
            // 返回cell
            return cell;
        }
            break;
            
        case 2:
        {
            // 实例化cell
            SurnameOfPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"picCell" forIndexPath:indexPath];
            
            // 设置选中cell的背景颜色
            UIView *selectedBackgroundView = [[UIView alloc] init];
            selectedBackgroundView.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = selectedBackgroundView;
            
            // 获取对应的一行的模型
            SurnameOfPicInfo *surnameOfPicInfo = [[SurnameManager managerDefault] getSurnameOfPicInfoWithIndex:[SurnameManager managerDefault].surnameOfPicCount - 1 - indexPath.row];
            
            // 重写setter
            cell.surnameOfPicInfo = surnameOfPicInfo;
            
            // 返回cell
            return cell;
        }
            break;
            
        case 3:
        {
            // 实例化cell
            SurnameOfVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videoCell" forIndexPath:indexPath];
            
            // 设置选中cell的背景颜色
            UIView *selectedBackgroundView = [[UIView alloc] init];
            selectedBackgroundView.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = selectedBackgroundView;
            
            // 获取对应的一行的模型
            SurnameOfVideoInfo *surnameOfVideoInfo = [[SurnameManager managerDefault] getSurnameOfVideoInfoWithIndex:[SurnameManager managerDefault].surnameOfVideoCount - 1 - indexPath.row];
            
            // 重写setter
            cell.surnameOfVideoInfo = surnameOfVideoInfo;
            
            NSLog(@"%ld", indexPath.row);
            
            // 返回cell
            return cell;
        }
            break;
            
        default:
            break;
    }
    
    
    
    return nil;
}

/*
#pragma mark - 点击手势触发事件
- (void)didClickTapAction:(NSUInteger)tap
{
    // 实例化player属性
    self.player = [[AVPlayer alloc] init];
    
    // 获取单例的中解析的player
    self.player = [[SurnameManager managerDefault] prepareWithIndex:self.index];
    
    // 实例化playerLayer
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    
    // 播放
    [self.player play];

    NSLog(@"点击手势 %ld", self.index);
    
}
*/
 
#pragma mark - 多种cell混合使用的实例化cell的方法

- (void)getSurnameTextCell
{
    
}


#pragma mark - lazy 

- (AVPlayerLayer *)playerLayer
{
    if (_playerLayer == nil)
    {
        _playerLayer = [[AVPlayerLayer alloc] init];
    }
    
    return _playerLayer;
}


#pragma mark - scrollView delegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // 暂停
    [self.player pause];
    
    // 清除playerLayer
    [self.playerLayer removeFromSuperlayer];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
